﻿package mortal.game.control
{
    import Message.Public.*;
    import com.mui.utils.*;
    import extend.language.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.view.common.*;
    import mortal.game.view.guildWar.*;
    import mortal.mvc.core.*;

    public class GuildWarController extends Controller
    {
        private var _lastGuildWarScore:SGuildWarScore;
        private var _leftTimeView:LeftTimeView;
        private var isInGuildWar:Boolean = false;

        public function GuildWarController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildWarOpen, this.onGuildWarOpen);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarClose, this.onGuildWarClose);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarInfo, this.onGuildWarInfo);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarGuildScore, this.onGuildWarGuildScore);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarPlayerScore, this.onGuildWarPlayerScore);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarResult, this.onGuildWarResult);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarSingleTime, this.onGuildWarSingleTime);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdate);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.GuildWarRequstList, this.onRequstList);
            Dispatcher.addEventListener(EventName.GuildWarRequestSignUp, this.onRequestSignUp);
            Dispatcher.addEventListener(EventName.GuildWarRequestEnter, this.onRequestEnter);
            Dispatcher.addEventListener(EventName.GuildWarRequestRankList, this.onRequestRankList);
            Dispatcher.addEventListener(EventName.GuildWarEnterWindowShow, this.onEnterWindowShow);
            Dispatcher.addEventListener(EventName.GuildWarGetVIPExploit, this.onGetVIPExploit);
            Dispatcher.addEventListener(EventName.GuildWarGetWinGift, this.onGetWinGift);
            return;
        }// end function

        protected function onRoleLevelUpdate(param1) : void
        {
            if (cache.guildWar.guildWarOpen && cache.role.entityInfo.level >= 38 && !this.isInGuildWar)
            {
                GuildWarIcon.instance.show();
                GuildWarIcon.instance.playEffect();
            }
            return;
        }// end function

        protected function onGuildWarOpen(param1:SGuildWarOpen) : void
        {
            if (!this.isInGuildWar && cache.role.entityInfo.level >= 38)
            {
                GuildWarIcon.instance.show();
                GuildWarIcon.instance.playEffect();
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarEnterWindowShow, true));
            }
            return;
        }// end function

        protected function onGuildWarClose(param1) : void
        {
            if (GuildWarIcon.hasInstance())
            {
                GuildWarIcon.instance.hide();
            }
            if (GuildWarEnterWindowNew.hasInstance())
            {
                GuildWarEnterWindowNew.instance.hide();
            }
            return;
        }// end function

        protected function onGuildWarInfo(param1:SGuildWarInfo) : void
        {
            if (!this.isInGuildWar && cache.role.entityInfo.guildId.id)
            {
                GuildWarEnterWindowNew.instance.show();
                GuildWarEnterWindowNew.instance.updateGuildWarInfo(param1);
            }
            return;
        }// end function

        protected function onGuildWarGuildScore(param1:SGuildWarScore) : void
        {
            if (GuildWarMiniView.hasInstance() && !GuildWarMiniView.instance.isHide)
            {
                GuildWarMiniView.instance.updateGuildScore();
            }
            this._lastGuildWarScore = param1;
            return;
        }// end function

        protected function onGuildWarPlayerScore(param1) : void
        {
            if (GuildWarMiniView.hasInstance() && !GuildWarMiniView.instance.isHide)
            {
                GuildWarMiniView.instance.updatePlayerScore(cache.guildWar.myScore);
            }
            return;
        }// end function

        public function get leftTimeView() : LeftTimeView
        {
            if (!this._leftTimeView)
            {
                this._leftTimeView = UICompomentPool.getUICompoment(LeftTimeView);
            }
            return this._leftTimeView;
        }// end function

        protected function onGuildWarSingleTime(param1) : void
        {
            if (GuildWarMiniView.hasInstance() && !GuildWarMiniView.instance.isHide)
            {
                GuildWarMiniView.instance.updateLeftTime();
            }
            return;
        }// end function

        protected function onGuildWarResult(param1:SGuildWarResult) : void
        {
            GuildWarResultWindowNew2.instance.show();
            GuildWarResultWindowNew2.instance.updateResult(param1);
            return;
        }// end function

        protected function onSceneUpdate(event:DataEvent) : void
        {
            if (GameMapUtil.curMapState.isGuildWar)
            {
                if (!this.isInGuildWar)
                {
                    this.isInGuildWar = true;
                    if (GuildWarIcon.hasInstance())
                    {
                        GuildWarIcon.instance.stopEffect();
                        GuildWarIcon.instance.hide();
                    }
                    if (GuildWarEnterWindowNew.hasInstance())
                    {
                        GuildWarEnterWindowNew.instance.hide();
                    }
                    GuildWarMiniView.instance.show();
                    GuildWarMiniView.instance.updateAll();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
                }
            }
            if (this.isInGuildWar && !GameMapUtil.curMapState.isGuildWar)
            {
                this.isInGuildWar = false;
                cache.guildWar.clearEnemyScore();
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
                if (GuildWarMiniView.hasInstance())
                {
                    GuildWarMiniView.instance.hide();
                }
                if (cache.guildWar.guildWarOpen)
                {
                    GuildWarIcon.instance.show();
                }
                cache.role.roleEntityInfo.clearGuildWarGuildNameJs();
                if (this._leftTimeView)
                {
                    this.leftTimeView.dispose();
                    this._leftTimeView = null;
                }
            }
            return;
        }// end function

        private function onRequstList(event:DataEvent) : void
        {
            if (!cache.role.entityInfo.guildId.id)
            {
                MsgManager.showRollTipsMsg(Language.getString(20406));
                return;
            }
            GameProxy.guildWarProxy.getGuildWarInfo();
            return;
        }// end function

        private function onRequestEnter(event:DataEvent) : void
        {
            if (!cache.role.entityInfo.guildId.id)
            {
                MsgManager.showRollTipsMsg(Language.getString(20406));
                return;
            }
            GameProxy.guildWarProxy.enterGuildWar();
            return;
        }// end function

        private function onRequestRankList(event:DataEvent) : void
        {
            GameProxy.guildWarProxy.getPlayerScores();
            return;
        }// end function

        private function onEnterWindowShow(event:DataEvent) : void
        {
            var _loc_2:* = !(event.data && Boolean(event.data));
            if (GuildWarIcon.hasInstance() && !GuildWarIcon.instance.isHide)
            {
                if (GuildWarEnterWindowNew.instance.isHide)
                {
                    if (!cache.role.entityInfo.guildId.id)
                    {
                        if (!_loc_2)
                        {
                            MsgManager.showRollTipsMsg(Language.getString(20406));
                        }
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarRequstList));
                        GuildWarEnterWindowNew.instance.show();
                    }
                }
                else if (!_loc_2)
                {
                    GuildWarEnterWindowNew.instance.hide();
                }
            }
            else if (!_loc_2)
            {
                MsgManager.showRollTipsMsg(Language.getString(20407));
            }
            return;
        }// end function

        private function onGetVIPExploit(event:DataEvent) : void
        {
            GameProxy.guildWarProxy.getGuildWarVIPExploit();
            return;
        }// end function

        private function onGetWinGift(event:DataEvent) : void
        {
            GameProxy.guildWarProxy.getGuildWarWinGift();
            return;
        }// end function

        private function onRequestSignUp(event:DataEvent) : void
        {
            if (!cache.role.entityInfo.guildId.id)
            {
                MsgManager.showRollTipsMsg(Language.getString(20406));
                return;
            }
            GameProxy.guildWarProxy.signUpGuildWar();
            return;
        }// end function

    }
}
